<template>
  <div class="login-form-wrapper">
    <div class="login-form-title">扫码登录</div>
    <div class="login-form-sub-title">请使用i黄冈扫码登录</div>
    <div class="login-form-error-msg">{{ errorMessage }}</div>
    <a-form
      ref="loginForm"
      :model="userInfo"
      class="login-form"
      layout="vertical"
      @submit="handleSubmit"
    >
      <qrcode-vue :value="qrCode" :size="qsize" />

    </a-form>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive,onMounted, onUnmounted } from 'vue';
  import { useRouter } from 'vue-router';
  import { Message } from '@arco-design/web-vue';
  import { ValidatedError } from '@arco-design/web-vue/es/form/interface';
  import { useI18n } from 'vue-i18n';
  import { useStorage } from '@vueuse/core';
  import { useUserStore } from '@/store';
  import useLoading from '@/hooks/loading';
  import { postBase } from '@/api/common';
  import QrcodeVue from "qrcode.vue";
  import {setToken} from '@/utils/auth';
  const router = useRouter();
  const { t } = useI18n();
  const errorMessage = ref('');
  const { loading, setLoading } = useLoading();
  const userStore = useUserStore();
  const qrCode = ref("");
  const qsize = ref(200);
  const userInfo = reactive({
    uuid: "",
  });
  const getQcode = async () => {
    let res:any = await postBase("dsms/jyData/getQrCode", {
    });
    if(res.code != 200){
      errorMessage.value = res.message;
      return;
    }
    qrCode.value = res.result;
  };
  getQcode();

  // 定时轮训 
  const intervalId = ref();
  //加载
  onMounted(async () => {
      // 设置定时器,定时保存
      intervalId.value = setInterval(() => {
          // 执行要重复执行的逻辑
          handleSubmit();//保存
      }, 5 * 1000);//每五分钟自动保存一下
  });
  onUnmounted(() => {
      // 清除定时器
      if (intervalId.value) {
          clearInterval(intervalId.value);
      }
  });

  const handleSubmit = async () => {
    try {
      let res:any = await postBase("dsms/jyData/loginCheck", {
        uuid:qrCode.value,
      });
      if(res.code != 200){
        // errorMessage.value = res.message;
        return;
      }
      let role = res.result.userRole;
      localStorage.setItem("userName",res.result.department +"-"+ res.result.realName);
      localStorage.setItem("userRole",role);
      localStorage.setItem("department",res.result.department);
      localStorage.setItem("project",res.result.project);
      setToken(res.result.phone);
      userStore.switchRoles(role)
      const { redirect, ...othersQuery } = router.currentRoute.value.query;
      router.push({
        name: (redirect as string) || 'reportStatic',
        query: {
          ...othersQuery,
        },
      });
      if (intervalId.value) {
          clearInterval(intervalId.value);
      }
      Message.success(t('login.form.login.success'));
    } catch (err) {
      // errorMessage.value = (err as Error).message;
    } 
  };
 
</script>

<style lang="less" scoped>
  .login-form {
    &-wrapper {
      width: 320px;
    }

    &-title {
      color: var(--color-text-1);
      font-weight: 500;
      font-size: 24px;
      line-height: 34px;
    }

    &-sub-title {
      color: var(--color-text-3);
      font-size: 16px;
      line-height: 24px;
    }

    &-error-msg {
      height: 32px;
      color: rgb(var(--red-6));
      line-height: 32px;
    }

    &-password-actions {
      display: flex;
      justify-content: space-between;
    }

    &-register-btn {
      color: var(--color-text-3) !important;
    }
  }
</style>
